package top.bingoz.wanandroid.ui.activity

import android.content.Context
import android.support.v7.widget.RecyclerView
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.VirtualLayoutManager
import kotlinx.android.synthetic.main.act_images.*
import top.bingoz.wanandroid.R
import top.bingoz.wanandroid.base.BaseActivity
import top.bingoz.wanandroid.bean.Images
import top.bingoz.wanandroid.data.http.observer.SimpleSubscriber
import top.bingoz.wanandroid.data.http.transformer.SimpleTransformer
import top.bingoz.wanandroid.di.module.HttpModule
import top.bingoz.wanandroid.ui.adapter.ImageAdapter
import java.util.*


class ImagesActivity : BaseActivity() {

    val mContext: Context = this

    var page: Int = 0

    private lateinit var mImgsAdapter: ImageAdapter


    override fun getLayoutId(): Int {
        return R.layout.act_images
    }

    override fun initialize() {


        initRecyclerView(recyclerView)


        refreshLayout.setOnRefreshListener {
            page = 0
            HttpModule.apiService(mContext).queryImages(page)
                    .compose<Images>(SimpleTransformer(provider.bindToLifecycle()))
                    .subscribe(object :SimpleSubscriber<Images>(){
                        override fun onNext(data: Images) {
                            super.onNext(data)

                            it.finishRefresh(true)

                            mImgsAdapter.setDatas(data.data)

                        }

                        override fun onError(e: Throwable) {
                            super.onError(e)
                            it.finishRefresh(false)
                        }
                    })
        }

        refreshLayout.setOnLoadMoreListener {
            page++
            HttpModule.apiService(mContext).queryImages(page)
                    .compose<Images>(SimpleTransformer(provider.bindToLifecycle()))
                    .subscribe(object :SimpleSubscriber<Images>(){
                        override fun onNext(data: Images) {
                            super.onNext(data)

                            it.finishLoadMore(true)

                            mImgsAdapter.addDatas(data.data)

                        }

                        override fun onError(e: Throwable) {
                            super.onError(e)
                            it.finishLoadMore(false)
                        }
                    })

        }

    }

    private fun initRecyclerView(view: RecyclerView) {
        val layoutManager = VirtualLayoutManager(mContext)
        // 注意：当hasConsistItemType=true的时候，不论是不是属于同一个子adapter，相同类型的item都能复用。表示它们共享一个类型
        // 当hasConsistItemType=false的时候，不同子adapter之间的类型不共享
        val delegateAdapter = DelegateAdapter(layoutManager)
        val adapters = LinkedList<DelegateAdapter.Adapter<out RecyclerView.ViewHolder>>();
        initAdapters(adapters)
        delegateAdapter.setAdapters(adapters);
        view.setLayoutManager(layoutManager);
        view.setAdapter(delegateAdapter);


    }

    private fun initAdapters(adapters: LinkedList<DelegateAdapter.Adapter<out RecyclerView.ViewHolder>>) {

        mImgsAdapter = ImageAdapter(mContext)

        adapters.add(mImgsAdapter)
    }


}
